﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FwProject.Authorization.Users;
using FwProject.Users;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Controllers
{
    [Route("api/[controller]/[action]")]
    public class UpLoadContorller : FwProjectControllerBase
    {
        private readonly IHostingEnvironment env;

        private readonly IUserAppService _userAppService;
        public UpLoadContorller(IHostingEnvironment _env, IUserAppService userAppService)
        {
            env = _env;
            _userAppService = userAppService;
        }

        [HttpPost]
        public async  Task<IActionResult> UpLoadFace(IFormCollection image)
        {
            Logger.Debug("测试上传");

            var model = new User();
            string path = string.Empty;
          
            var files = image.Files;
            if (files == null || files.Count() <= 0)
            {
                return Json("请上传头像！");
            }
            //格式限制
            var allowType = new string[] { "image/jpg", "image/png", "image/jpeg", };
            if (files.Any(c => allowType.Contains(c.ContentType)))
            {
                try
                {
                    if (files.Sum(c => c.Length) <= 1024 * 1024 * 4)
                    {
                        var file = files.FirstOrDefault();
                        string strpath = Path.Combine("UploadFace", DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(file.FileName));
                        //foreach (var file in files)
                        //{
                        //  string strpath = Path.Combine("Upload", DateTime.Now.ToString("MMddHHmmss") + file.FileName);
                        path = Path.Combine(env.WebRootPath, strpath);
                        var tempPath = Path.Combine(env.WebRootPath, "UploadFace");
                        if (!Directory.Exists(tempPath))
                            Directory.CreateDirectory(tempPath);

                        Logger.Debug($"{tempPath}======={env.WebRootPath}");
                        Logger.Debug(path);

                        using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                          await  file.CopyToAsync(stream);
                        }
                        // }
                        await _userAppService.EditFace(strpath);
                        return Json(strpath);
                    }
                    else
                    {

                        return Json("图片过大");
                    }
                }
                catch (Exception exe)
                {
                  
                    Logger.Error(exe.ToString());
                    return Json(exe.ToString());
                }
               
                
            }
            else
            {

                return Json("图片格式错误！");
            }
          
        }
    }
}
